﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>属性访问错误</title>
    <script>
        // 获取book的subTitle的length
        var book = {
            subTitle: "abc"
        };
        //一个冗余的方法
        function getLength1() {
            if (book) {
                if (book.subTitle) {
                    alert(book.subTitle.length);
                }
            }
        }
        //一个常用的方法
        function getLength2() {
            if (book && book.subTitle && book.subTitle.length) {
                var len = book && book.subTitle && book.subTitle.length;
                alert(len);
            }
        }


    </script>
</head>
<body onload="getLength2();">

</body>
</html>